<template>
  <div id="change">
    <!-- 用户的封面 -->
    <div class="cover">
      <!-- 顶部导航栏 -->
      <div>
        <van-icon name="arrow-left" class="left" @click="onClickLeft"/>
      </div>
      <!-- 用户的头像 昵称 -->
      <div class="img">
        <van-image
          round
          fit="cover"
          :src="users.img"
          class="image"
        />
      </div>
      <div class="name">
         {{users.name}}
         <!-- 用户的认证图标 -->
        <van-image v-if="users.student_identification" :src="require('../../assets/book.png')" class="identify" />
        <van-icon name="manager" v-if="users.people_identification" color="#00a8f3"/>
        <van-icon name="shop" v-if="users.shop_identification" color="#ff000a"/>
      </div>
      <!-- 用户的个性签名或者个人信息 -->
      <div class="signature" @click="signatureShow = true">
        <van-icon name="edit" class="edit"/>
        {{users.signature}}
      </div>
        <!-- 点击个性签名后的修改框 -->
      <van-dialog v-model="signatureShow" title="修改个性签名" show-cancel-button @confirm="signatureConfirm">
        <van-cell-group>
          <van-field v-model="signatureValue" :placeholder="users.signature" />
        </van-cell-group>
      </van-dialog>
    </div>
    <!-- 用户的信息 -->
    <van-cell-group>
      <!-- 用户名 -->
      <van-cell title="悬赏人昵称" :value="users.name" @click="nameShow = true"/>
        <!-- 点击昵称后的修改框 -->
      <van-dialog v-model="nameShow" title="修改用户名" show-cancel-button @confirm="nameConfirm">
        <van-cell-group>
          <van-field v-model="nameValue" :placeholder="users.name" />
        </van-cell-group>
      </van-dialog>
      <!-- QQ -->
      <van-cell title="QQ" :value="users.qq_number" @click="qqShow = true"/>
        <!-- 点击QQ后的修改框 -->
      <van-dialog v-model="qqShow" title="修改QQ号" show-cancel-button @confirm="qqConfirm">
        <van-cell-group>
          <van-field v-model="qqValue" :placeholder="users.qq_number" />
        </van-cell-group>
      </van-dialog>
      <!-- 电话 -->
      <van-cell title="电话" :value="users.phone_number" @click="phoneShow = true"/>
        <!-- 点击电话后的修改框 -->
      <van-dialog v-model="phoneShow" title="修改电话号" show-cancel-button @confirm="phoneConfirm">
        <van-cell-group>
          <van-field v-model="phoneValue" :placeholder="users.phone_number" />
        </van-cell-group>
      </van-dialog>
    </van-cell-group>
    <!-- 上传头像的input框 -->
   <van-field name="uploader" label="修改头像">
      <template #input>
      <div class="uploaderImg"></div>
        <van-uploader :after-read="afterRead"/>
      </template>
    </van-field>
  </div>
  
</template>

<script>
import { Toast } from 'vant';
export default {
  name: 'Change',
  data () {
    return {
      users: {},
      credit: null,
      // 修改框的出现
      headShow: false,
      signatureShow: false,
      nameShow: false,
      qqShow: false,
      phoneShow: false,
      // 修改的内容存储
      signatureValue: '',
      nameValue: '',
      qqValue: '',
      phoneValue: '',
      fileCount:""
    }
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1)
    },
    imgChange(e) {
      this.imgValue = window.URL.createObjectURL(e.target.files[0])
    },
    headConfirm() {
      this.users.img = this.imgValue
    },
    signatureConfirm() {
      // 这里修改个性签名
      this.users.signature = this.signatureValue
      let params = new URLSearchParams();
            params.append('signature',  this.users.signature);
            this.axios({
                method: "put",
                url: this.$store.state.url+"/auth/user/updateInfo",
                data: params,
                headers:{
                    'Authorization': localStorage.token
                }
            }).then((res) => {
                console.log(res.data);
            }).catch((err) => {
                console.log(err);
            })
    },
    nameConfirm() {
      // 修改用户名
      this.users.name = this.nameValue
      let params = new URLSearchParams();
            params.append('name', this.users.name);
            this.axios({
                method: "put",
                url: this.$store.state.url+"/auth/user/updateInfo",
                data: params,
                headers:{
                    'Authorization': localStorage.token
                }
            }).then((res) => {
              if(res.data.data.code=="A0111"){
                this.nameValue = "",
                Toast(res.data.data.message);
              }
                console.log(res.data);
            }).catch((err) => {
                console.log(err);
            })
    },
    qqConfirm() {
      // 修改QQ
      this.users.qq_number = this.qqValue
      let params = new URLSearchParams();
            params.append('qq_number', this.users.qq_number);
            this.axios({
                method: "put",
                url: this.$store.state.url+"/auth/user/updateInfo",
                data: params,
                headers:{
                    'Authorization': localStorage.token
                }
            }).then((res) => {
                console.log(res.data);
            }).catch((err) => {
                console.log(err);
            })
    },
    phoneConfirm() {
      // 修改电话号
      this.users.phone_number = this.phoneValue
      let params = new URLSearchParams();
            params.append('phone_number',  this.users.phone_number);
            this.axios({
                method: "put",
                url: this.$store.state.url+"/auth/user/updateInfo",
                data: params,
                headers:{
                    'Authorization': localStorage.token
                }
            }).then((res) => {
                console.log(res.data);
            }).catch((err) => {
                console.log(err);
            })
    },
    imgConfirm(){
      let params = new URLSearchParams();
      console.log(this.fileCount)
      params.append('img',this.fileCount);
            this.axios({
                method: "put",
                url: this.$store.state.url+"/auth/user/updateInfo",
                data: params,
                headers:{
                    'Authorization': localStorage.token
                }
            }).then((res) => {
               this.user();
               window.location.reload();
                console.log(res.data);
            }).catch((err) => {
                console.log(err);
            })
    },
      afterRead(file) {
      // 此时可以自行将文件上传至服务器
      var formData = new FormData();
      formData.append('file',file.file);
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
      });
      this.axios({
          method: "post",
          url: this.$store.state.url+"/picture",
          headers:{
           "Content-Type": "multipart/form-data",
           'Authorization': localStorage.token,
                },
          data:formData,
      }).then((res) => {
        console.log(res.data);
        Toast.success('上传成功');
        Toast.clear();
        this.fileCount = res.data.data.file_name;
        this.imgConfirm();  
      }).catch((err) => {
          console.log(err);
      })
      
    },
    user(){
       this.axios({
        method: "get",
        url: this.$store.state.url+"/auth/user/info",
        headers:{
           'Authorization': localStorage.token
                }
      }).then((res) => {
        console.log(res.data.data);
        this.users = res.data.data;
        this.credit = this.users.credit_score/2;
        if(res.data.data.img){
        this.users.img = "http://picture.105577.xyz/"+res.data.data.img;
        }
      }).catch((err) => {
        console.log(err);
      })
    }
  },
  created(){
    this.user();
  },
}
</script>

<style scoped>
  .navbar {
    color: #ffffff;
  }
  .cover {
    width: 100%;
    height: 240px
  }
  .left {
    font-size: 20px;
    color: #fff;
    padding: 5px;
    margin: 15px
  }
  .report {
    position: relative;
    right: 0;
    padding: 5px;
    margin: 14px;
    float: right;
    font-weight: 510;
    color: #fff;
  }
  .img {
    text-align: center;
    margin: 15px 0;
  }
  .image {
    width: 80px;
    height: 80px;
    border: 1px solid #cccccc
  }
  .identify {
    width: 18px;
    height: 18px
  }
  .name {
    text-align: center;
    font-weight: 900;
    line-height: 10px;
    color: rgb(255, 255, 255);
  }
  .signature {
    text-align: center;
    margin: 0 auto;
    padding: 15px;
    overflow: hidden;
    font-size: 13px;
    color: #fff;
  }
  .edit {
    font-size: 18px;
    vertical-align: text-bottom
  }
  .uploaderImg {
    margin-left: 173px
  }
  .identity {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 20px;
  }
</style>